<template>
  <div>
    <div style="height: 70vh; width:100%; overflow-y: auto;" v-if="pdfUrl != null">
      <iframe :src="pdfUrl" width="100%" height="100%"  frameborder="0" scrolling="yes" ></iframe>
    </div>
    <el-form
      class="input-form"
      label-position="right"
      label-width="120px"
      ref="editForm"
      :rules="propRules"
      :disabled="propReadonly"
      :model="form"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="日期" prop="balanceDate">
            <el-date-picker v-model="form.balanceDate" type="date" :format="dates.defaultDatePattern"  :picker-options="dates.pickerOptions.date"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存地" prop="warehouseName">
            <el-select v-model="form.warehouseName" filterable>
              <el-option value="">请选择</el-option>
              <el-option v-for="dict in propDictionaries['warehouseName']"  :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="科室" prop="deptName">
            <el-select v-model="form.deptName" filterable>
              <el-option value="">请选择</el-option>
              <el-option v-for="dict in propDictionaries['deptName']"  :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <div style="height:500px;overflow:auto;">
          <data-view
            v-show="propStatus==='edit'"
            propViewKey="dept_daily_balance_item"
            propViewForm="dept/dailybalanceitem/detail.vue"
            :propInitData="form"
            ref="childDataView"
            :propHidePageSearch="true"
            :propHideQueryButton="true"
            :propParentPage="_self"
            :propAutoFetchData="false"
          />
        </div>
      </el-row>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      form: {},
      pdfUrl: null,
      viewData: {}
    }
  },
  methods: {
    getChildViewQueryWrapper() {
      if (this.form.id) {
        return this.$newWrapper().eq('deptDailyBalanceId', this.form.id)
      } else {
        return this.$newWrapper().ne(1, 1)
      }
    },
    getChildRows() {
      if (!this.$refs.childDataView) {
        return []
      }
      return this.$refs.childDataView.getRows0()
    },
    //打印预览
    printPreview() {
      this.pdfUrl = `api/dept/daily_balance/preview?id=${this.form.id}`
    },
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)

        // 设置switch 元素默认值

        let wrapper = this.getChildViewQueryWrapper()
        this.$refs.childDataView && this.$refs.childDataView.setQueryWrapper(wrapper)
      },
      afterSave: () => {},
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        that.pdfUrl = null
        return true
      },
      afterClick(button, data) {
        let {key: buttonKey} = button
        if (buttonKey == 'preview') {
          if (that.pdfUrl == null) {
            that.printPreview()
          }
        }
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {
  },
  computed: {
    cpChildRows() {
      if (!this.$refs.childDataView) {
        return []
      }
      return this.$refs.childDataView.getRows0()
    }
  },
  watch: {

  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>
